// pages/business/myagent/myagent.js
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    page_size: 20,
    agentflag: true,
    showLoading: true,
    agent_list: [],
    brand_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // index 1-我的代理商  2-申请返货
    var title = '';
    this.setData({
      currentIndex: options.index
    })
    if (options.index == 1) {
      title = '我的代理商';
      this.refreshlist();
    } else if (options.index == 2 || options.index == 3) {
      title = options.index == 2 ? '申请返货' : '全部品牌';
      this.getBrandList();
    } else if (options.index == 4) {
      title = '代理商的统计';
      this.refreshlist();
    }
    wx.setNavigationBarTitle({
      title: title,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 页面事件
   */
  refreshlist: function () { //agent_child_list
    var that = this, list, url = '';
    if (that.data.currentIndex == 1) {
      url = config.agent_child_list;
    } else if (that.data.currentIndex == 4) {
      url = config.agent_report_child_list;
    }
    shihan.sendRequest(url, 'POST', {
        page: that.data.page,
        page_size: that.data.page_size
      })
      .then(function(response) {
        if (response.data.code == 0) {
          // console.log(response);
          if (that.data.currentIndex == 1) {
            list = response.data.data.sh_data;
          } else if (that.data.currentIndex == 4) {
            list = response.data.data.sh_items;
          }
          if (list.length > 0) {
            that.setData({
              page: that.data.page + 1,
              agent_list: that.data.agent_list.concat(list),
              showLoading: false,
              agentflag: true
            })
          } else {
            that.setData({
              showLoading: false,
              agentflag: false
            })
          }
          if (that.data.agent_list.length == response.data.data.sh_total_rows) {
            that.setData({
              showLoading: false,
              agentflag: false
            })
          }
        } else {
          that.setData({
            showLoading: false,
            agentflag: false
          })
        }
        wx.stopPullDownRefresh();
      }, function(error) {
        console.log(error);
      });
  },
  agentInfo: function(e) {
    var index = this.data.currentIndex == 1 ? 2 : 4;
    wx.navigateTo({
      url: '/pages/business/myagentinfo/myagentinfo?index=' + index +'&id=' + e.currentTarget.dataset.id,
    })
  },
  getBrandList: function () {//agent_return_brand_list
    var that = this, url = '';
    if(this.data.currentIndex == 2) {
      url = config.agent_return_brand_list
    } else if (this.data.currentIndex == 3) {
      url = config.agent_brand_all
    }
    shihan.sendRequest(url, 'POST', {})
      .then(function (response) {
        if (response.data.code == 0) {
          // console.log(response);
          if (response.data.data.sh_items.length > 0) {
            that.setData({
              brand_list: that.data.brand_list.concat(response.data.data.sh_items)
            })
          }
        }
        that.setData({
          showLoading: false
        })
        wx.stopPullDownRefresh();
      }, function (error) {
        console.log(error);
      });
  },

  agentReturn: function (e) {
    var status = e.currentTarget.dataset.status,
      name = e.currentTarget.dataset.name,
      url = e.currentTarget.dataset.url,
      id = e.currentTarget.dataset.id,
      tit = e.currentTarget.dataset.tit;
    if (url) {
      if (status == 1) { //查看品牌介绍，可以代理
        wx.navigateTo({
          url: '/pages/strategyinfo/strategyinfo?share=0&nologin=1&url=' + encodeURIComponent(url) + '&name=' + name
        })
      } else if (status == 2) { //暂不能代理
        shihan.showToast.call(this, '该品牌即将入驻，敬请期待');
      }
    } else {
      wx.navigateTo({
        url: '/pages/business/agentreturn/agentreturn?id=' + id + '&tit=' + tit,
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    if (this.data.currentIndex == 1 || this.data.currentIndex == 4) {
      this.setData({
        page: 1,
        agent_list: [],
        showLoading: true
      });
      this.refreshlist()
    } else if (this.data.currentIndex == 2 || this.data.currentIndex == 3) {
      this.setData({
        brand_list: [],
        showLoading: true
      });
      this.getBrandList()
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.currentIndex == 1 || this.data.currentIndex == 4) {
      if (this.data.agentflag) {
        this.setData({
          agentflag: false
        })
        this.refreshlist()
      }
    }
  }
})